package gdou.gym.service.equipment.impl;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import gdou.gym.dao.EqRentMapper;
import gdou.gym.dao.EqmentMapper;
import gdou.gym.entity.EqRent;
import gdou.gym.entity.EqRentExample;
import gdou.gym.entity.Eqment;
import gdou.gym.entity.EqmentExample;
import gdou.gym.exception.StateCode;
import gdou.gym.exception.SysException;
import gdou.gym.service.equipment.EqService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EqServiceImpl implements EqService {
    @Resource
    private EqmentMapper eqmentMapper;

    @Resource
    private EqRentMapper eqRentMapper;


    @Override
    public int addEqAsk(Map<String, Object> map, String account) {
        String no = (String) map.get("no");
        Integer count = (Integer) map.get("count");
        int key = eqmentMapper.updateRemainByPrimaryKey(no, count);
        if (key == 0) throw new SysException(StateCode.ORDER_ERROR);
        EqRent record = new EqRent();
        double rentCost;
        Object cost = map.get("rentCost");
        if (!(cost instanceof Number)) throw new SysException(StateCode.PARAMS_ERROR);
        rentCost = ((Number) cost).doubleValue();
        record.setCount(count);
        record.setAccount(account);
        record.setTimestamp(new Date());
        record.setEqNo(no);
        record.setCost(rentCost * count);
        return eqRentMapper.insert(record);

    }

    @Override
    public int addEq(Eqment eqment) {
        return eqmentMapper.insert(eqment);
    }

    @Override
    public int delEq(String eqNo) {
        return eqmentMapper.deleteByPrimaryKey(eqNo);
    }

    @Override
    public int updateEq(Eqment eqment) {
        return eqmentMapper.updateByPrimaryKey(eqment);
    }

    @Override
    public List<Eqment> query() {
        return eqmentMapper.selectByExample(null);
    }

    @Override
    public List<?> queryMyRent(String account) {
        EqRentExample example = new EqRentExample();
        example.createCriteria().andAccountEqualTo(account);
        List<EqRent> rents = eqRentMapper.selectByExample(example);
        if (rents.isEmpty()) return rents;
        EqmentExample ee = new EqmentExample();
        ee.createCriteria().andNoIn(rents.stream().map(EqRent::getEqNo).collect(Collectors.toList()));
        List<Eqment> eqments = eqmentMapper.selectByExample(ee);
        HashMap<String, String> map = new HashMap<>();
        for (Eqment eqment : eqments) {
            map.put(eqment.getNo(), eqment.getName());
        }
        ArrayList<EqVo> vos = new ArrayList<>();
        for (EqRent rent : rents) {
            vos.add(new EqVo(map.get(rent.getEqNo()), rent));
        }
        return vos;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class EqVo {
        private String name;
        @JsonUnwrapped
        private EqRent rent;
    }

}
